package com.bkgy.modular.tbdsjrawcheckc642.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.excelhandle.PlanExcelHandlerImpl;
import com.bkgy.modular.tbdsjrawcheckc642.dao.TbDsjRawcheckC642CopyMapper;
import com.bkgy.modular.tbdsjrawcheckc642.entity.ExportTbDsjRawcheckC642Copy;
import com.bkgy.modular.tbdsjrawcheckc642.entity.TbDsjRawcheckC642Copy;
import com.bkgy.modular.tbdsjrawcheckc642.service.TbDsjRawcheckC642CopyService;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.ExcelUtilsPOI;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TbDsjRawcheckC642CopyServiceImpl extends ServiceImpl<TbDsjRawcheckC642CopyMapper, TbDsjRawcheckC642Copy> implements TbDsjRawcheckC642CopyService {

    @Resource
    TbDsjRawcheckC642CopyMapper tbDsjRawcheckC642CopyMapper;
    /**
     * 2130酸轧质量精度结果
     * @param json
     * @return
     */
    @Override
    public ResultData findByPage(JSONObject json) {
        Integer pageInex = 1;
        Integer pageSize = 10;
        pageInex = json.getInteger("pageIndex"); // 页数
        pageSize = json.getInteger("pageSize"); // 每页条数
        JSONObject condition = json.getJSONObject("condition");
        if (pageInex <= 0 || pageInex == null){
            pageInex = 1;
        }
        if(pageSize == null){
            pageSize = 10;
        }
        List<TbDsjRawcheckC642Copy> conditionList = tbDsjRawcheckC642CopyMapper.findByPage((pageInex - 1) * pageSize,pageSize * pageInex,condition);
        Integer dataCount = tbDsjRawcheckC642CopyMapper.findDataCount(condition);
        return ResultData.ResultDataSuccess(conditionList,dataCount);
    }

    @Override
    public void export(JSONObject jsonObject, HttpServletResponse response) {
        // 接收传入参数
        Integer pageSize = jsonObject.getInteger("pageSize");
        Integer pageCount = jsonObject.getInteger("pageCount");
        String fileNames = jsonObject.getString("fileName");
        JSONObject condition = jsonObject.getJSONObject("condition");

        // 创建表格数据集合
        List<TbDsjRawcheckC642Copy> listExcel = new ArrayList<>();

        // 调用service查询出数据
        List<TbDsjRawcheckC642Copy> data = null;
        for (int i = 1; i <= pageCount; i++) {
            // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
            data = tbDsjRawcheckC642CopyMapper.findByPage((i - 1) * pageSize, i * pageSize, condition);
            for (TbDsjRawcheckC642Copy item : data) {
                listExcel.add(item);
            }
        }
        List<ExportTbDsjRawcheckC642Copy> list = new ArrayList<>();
        for (TbDsjRawcheckC642Copy item : listExcel) {
            ExportTbDsjRawcheckC642Copy exportTbDsjRawcheckC642Copy = new ExportTbDsjRawcheckC642Copy();
            BeanUtils.copyProperties(item,exportTbDsjRawcheckC642Copy);
            list.add(exportTbDsjRawcheckC642Copy);
        }
        try {
            Map<String,Object> data1 = new HashMap();
            data1.put("data",list);
            File file = ResourceUtils.getFile("classpath:exceltemplate\\2130酸轧厚度精度.xlsx");
            TemplateExportParams templateExportParams = new TemplateExportParams(file.getPath(),false);
            templateExportParams.setDictHandler(new PlanExcelHandlerImpl());
            Workbook workbook = ExcelExportUtil.exportExcel(templateExportParams, data1);
            ExcelUtilsPOI.exportExcel(fileNames,response,workbook);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
